// 自动生成的JSON转换内容基础类
// 此文件包含JSON序列化和反序列化的核心功能

abstract class JsonConvert {
  /// 从JSON对象转换为指定类型T
  static T? fromJsonAsT<T>(dynamic json) {
    if (json == null) {
      return null;
    }
    
    // 处理基本类型
    if (T == String) {
      return json.toString() as T;
    } else if (T == int) {
      if (json is int) return json as T;
      if (json is double) return json.toInt() as T;
      if (json is String) return int.tryParse(json) as T?;
    } else if (T == double) {
      if (json is double) return json as T;
      if (json is int) return json.toDouble() as T;
      if (json is String) return double.tryParse(json) as T?;
    } else if (T == bool) {
      if (json is bool) return json as T;
      if (json is String) {
        return (json.toLowerCase() == 'true' || json == '1') as T;
      }
      if (json is int) return (json == 1) as T;
    } else if (T == List || T.toString().startsWith('List<')) {
      return (json as List) as T;
    } else if (T == Map || T == Map<String, dynamic>) {
      return (json as Map<String, dynamic>) as T;
    }
    
    // 对于复杂对象，返回原始JSON，让具体的模型类处理
    return json as T?;
  }
  
  /// 将对象转换为JSON
  static dynamic toJson(dynamic object) {
    if (object == null) {
      return null;
    }
    
    // 处理基本类型
    if (object is String || object is int || object is double || object is bool) {
      return object;
    }
    
    // 处理List
    if (object is List) {
      return object.map((item) => toJson(item)).toList();
    }
    
    // 处理Map
    if (object is Map) {
      return object.map((key, value) => MapEntry(key.toString(), toJson(value)));
    }
    
    // 对于有toJson方法的对象
    if (object.runtimeType.toString().contains('toJson')) {
      try {
        return (object as dynamic).toJson();
      } catch (e) {
        return object.toString();
      }
    }
    
    return object.toString();
  }
  
  /// 检查对象是否为空
  static bool isEmpty(dynamic object) {
    if (object == null) return true;
    if (object is String) return object.isEmpty;
    if (object is List) return object.isEmpty;
    if (object is Map) return object.isEmpty;
    return false;
  }
  
  /// 安全获取Map中的值
  static T? getValueSafely<T>(Map<String, dynamic>? map, String key) {
    if (map == null || !map.containsKey(key)) {
      return null;
    }
    return fromJsonAsT<T>(map[key]);
  }
}

/// JSON转换异常类
class JsonConvertException implements Exception {
  final String message;
  final dynamic originalValue;
  final Type targetType;
  
  const JsonConvertException({
    required this.message,
    this.originalValue,
    required this.targetType,
  });
  
  @override
  String toString() {
    return 'JsonConvertException: $message\n'
           'Original value: $originalValue\n'
           'Target type: $targetType';
  }
}

/// 日期时间转换工具
class DateTimeConverter {
  /// 将时间戳转换为DateTime
  static DateTime? fromTimestamp(dynamic timestamp) {
    if (timestamp == null) return null;
    
    try {
      if (timestamp is int) {
        // 判断是秒还是毫秒
        if (timestamp.toString().length <= 10) {
          return DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
        } else {
          return DateTime.fromMillisecondsSinceEpoch(timestamp);
        }
      }
      if (timestamp is String) {
        return DateTime.parse(timestamp);
      }
    } catch (e) {
      return null;
    }
    
    return null;
  }
  
  /// 将DateTime转换为时间戳
  static int? toTimestamp(DateTime? dateTime) {
    return dateTime?.millisecondsSinceEpoch;
  }
  
  /// 将DateTime转换为ISO字符串
  static String? toIsoString(DateTime? dateTime) {
    return dateTime?.toIso8601String();
  }
}



